<?php
namespace app\admin\logic;

use think\facade\View;

class  GoodsLogic
{
    //分类列表
    public static function newData($param)
    {
        $parent_id = isset($param['parent_id'])?$param['parent_id']:0;
        return $parent_id;
    }

    //属性列表
    //where条件
    public static function propWhere($param)
    {
        $where = [];
        $whereOr = [];
        //模糊搜索判断
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $where[] = ['pname','like','%'.$param['keywords'].'%'];
        }
        if (isset($param['vid']) && !empty($param['vid'])){
            foreach ($param['vid'] as $v){
                $whereOr[] = ['pvalues','find in set',$v];
            }
        }
        return [$where,$whereOr];
    }
    //留值
    public static function propData($param)
    {
        $data = [
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'vid'=>isset($param['vid'])?$param['vid']:[],
        ];
        return $data;
    }
    //属性值转中文
    public static function propValue($list,$pvalue)
    {
        foreach ($list as &$v){
            $v['pvalues'] = explode(',',$v['pvalues']);
            $str = [];
            foreach ($v['pvalues'] as $vv){
                foreach ($pvalue as $vvv){
                    if ($vv == $vvv['vid']){
                        $str[] = $vvv['prop_value'];
                    }
                }
            }
            $v['pvalues'] = implode(',',$str);
        }
        return $list;
    }


    //品牌列表
    //where条件
    public static function brandWhere($param)
    {
        $where = [];
        $whereOr = [];
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $where[] = ['brand_name','like','%'.$param['keywords'].'%'];
        }
        if (isset($param['recommended']) && $param['recommended']<>""){
            $where[] = ['recommended','=',$param['recommended']];
        }
        if (isset($param['if_show']) && count($param['if_show'])==1){
            $where[] = ['if_show','=',$param['if_show'][0]];
        }
        if (isset($param['logo']) && count($param['logo'])==1){
            $logo = implode(',',$param['logo']);
            if ($logo == 1){
                $where[] = ['brand_logo','<>',' '];
            }
            if ($logo == 0){
                $whereOr[] = ['brand_logo','=',' '];
                $whereOr[] = ['brand_logo','=',null];
            }
        }
        return [$where,$whereOr];
    }
    //留值
    public static function brandData($param)
    {
        $data = [
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'recommended'=>isset($param['recommended'])?$param['recommended']:'',
            'if_show'=>isset($param['if_show'])?$param['if_show']:[],
            'logo'=>isset($param['logo'])?$param['logo']:[]
        ];
        return $data;
    }

    //商品列表
    //where条件
    public static function goodsWhere($param)
    {
        $where = [];
        if (isset($param['keywords']) && !empty($param['keywords'])){
            $where[] = ['g.goods_name','like','%'.$param['keywords'].'%'];
        }
        if (isset($param['bid']) && !empty($param['bid'])){
            $where[] = ['b.brand_id','=',$param['bid']];
        }
        if (isset($param['cid']) && !empty($param['cid'])){
            $where[] = ['g.cate_id','=',$param['cid']];
        }
        if (isset($param['minTime']) && !empty($param['minTime'])){
            $min = strtotime($param['minTime']);
            $where[] = ['g.add_time','>=',$min];
        }
        if (isset($param['maxTime']) && !empty($param['maxTime'])){
            $max = strtotime($param['maxTime']);
            $where[] = ['g.add_time','<=',$max];
        }
        if (isset($param['minMoney']) && !empty($param['minMoney'])){
            $where[] = ['g.price','>=',$param['minMoney']];
        }
        if (isset($param['maxMoney']) && !empty($param['maxMoney'])){
            $where[] = ['g.price','<=',$param['maxMoney']];
        }
        if (isset($param['if_show']) && count($param['if_show'])==1){
            $where[] = ['g.if_show','=',$param['if_show'][0]];
        }
        if (isset($param['recommended']) && $param['recommended']<>""){
            $where[] = ['g.recommended','=',$param['recommended']];
        }
        return $where;
    }
    //时间排序
    public static function getOrderTime($param)
    {
        $order = ['goods_id'=>'asc'];
        View::assign(['admin'=>1,'msg'=>'']);
        if (isset($param['admin']) && $param['admin']==1){
            $order = ['g.add_time'=>'desc'];
            $msg = "↓";
            View::assign(['admin'=>2,'msg'=>$msg]);
        }
        if (isset($param['admin']) && $param['admin']==2){
            $order = ['g.add_time'=>'asc'];
            $msg = "↑";
            View::assign(['admin'=>0,'msg'=>$msg]);
        }
        return $order;
    }

    //留值
    public static function goodsData($param)
    {
        $data = [
            'keywords'=>isset($param['keywords'])?$param['keywords']:'',
            'bid'=>isset($param['bid'])?$param['bid']:'',
            'cid'=>isset($param['cid'])?$param['cid']:'',
            'minTime'=>isset($param['minTime'])?$param['minTime']:'',
            'maxTime'=>isset($param['maxTime'])?$param['maxTime']:'',
            'minMoney'=>isset($param['minMoney'])?$param['minMoney']:'',
            'maxMoney'=>isset($param['maxMoney'])?$param['maxMoney']:'',
            'recommended'=>isset($param['recommended'])?$param['recommended']:'',
            'if_show'=>isset($param['if_show'])?$param['if_show']:[],
        ];
        return $data;
    }
    //关键字变色
    public static function goodsKeywords($list,$param)
    {
        if (isset($param['keywords']) && !empty($param['keywords'])){
            foreach ($list as &$v){
                $word = '<font style="color:red">'.$param['keywords'].'</font>';
                $v['goods_name'] = str_replace($param['keywords'],$word,$v['goods_name']);
            }
        }
        return $list;
    }
    //商品添加
    public static function addGoodsInfo($post)
    {
        $data = [
            'goods_name'        =>$post['goods_name'],
            'cate_id'           =>$post['cid'],
            'brand'             =>$post['bid'],
            'price'             =>$post['price'],
            'vip'               =>$post['vip'],
            'spec_qty'          =>$post['spec_qty'],
            'recommended'       =>$post['recommended'],
            'if_show'           =>$post['if_show'],
            'recom'             =>$post['recom'],
            'description'       =>$post['content'],
            'add_time'          =>time()
        ];
        return $data;
    }
    //商品修改
    public static function editGoodsInfo($post)
    {
        $data = [
            'goods_id'          =>$post['goods_id'],
            'goods_name'        =>$post['goods_name'],
            'cate_id'           =>$post['cid'],
            'brand'             =>$post['bid'],
            'price'             =>$post['price'],
            'vip'               =>$post['vip'],
            'spec_qty'          =>$post['spec_qty'],
            'recommended'       =>$post['recommended'],
            'if_show'           =>$post['if_show'],
            'recom'             =>$post['recom'],
            'description'       =>$post['content'],
            'default_image'     =>$post['imgs'],
            'last_update'       =>time()
        ];
        return $data;
    }
    //属性添加
    public static function addPropInfo($post)
    {
        $data = [
            'pname'     =>$post['name'],
            'pvalues'   =>$post['vid'],
            'if_show'   =>$post['if_show'],
            'sort_order'=>$post['sort']
        ];
        return $data;
    }
    //属性分类添加
    public static function addPropsInfo($post)
    {
        $post['values'] = explode(',',$post['values']);
        foreach ($post['values'] as $v){
            $data[] = [
                'prop_value'   =>$v,
                'sort_order'=>$post['sort']
            ];
        }
        return $data;
    }
    //属性修改
    public static function editPropInfo($post)
    {
        $data = [
            'pid'       =>$post['pid'],
            'pname'     =>$post['name'],
            'pvalues'   =>$post['vid'],
            'if_show'   =>$post['if_show'],
            'sort_order'=>$post['sort']
        ];
        return $data;
    }








}